﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.Services;
using System.Runtime.InteropServices;

using System.Text;
using System.Configuration;
using System.Globalization;
using Slja.GSMUtility;
using log4net;

namespace Slja.WebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Sms : System.Web.Services.WebService
    {
      
        static Sms()
        {
        }

        static void GSMOnRecieved(object sender, EventArgs e)
        {
            Slja.GSMMODEM.GSMModem curGSM = (Slja.GSMMODEM.GSMModem)sender;
            int newMsgIdx = curGSM.newMsgIndex;
            string strMsg = curGSM.ReadMsgByIndex(newMsgIdx);
            string[] arr =  new string[] { strMsg };
            List<SmsMsgInfo> msgList = SmsUti.ConvertRecivedMsgFormat(arr);
            //生成Model数据库列表
            List<Slja.GSMUtility.Model.sms_MsgRecive> listModel = SmsUti.AssignReciveModelList(msgList);
            //保存数据库
            Slja.GSMUtility.DAL.sms_MsgRecive dal = new Slja.GSMUtility.DAL.sms_MsgRecive();
            List<int> listid = dal.Add(listModel);
        }

        [WebMethod]
        public List<Slja.GSMUtility.Model.sms_MsgSend> Send(List<string> sms_Reciver, string Msg, int SendUser)
        {
            //记录日志
            ILog logger = LogManager.GetLogger("File");
            logger.Info(DateTime.Now.ToString("HH:mm:ss fff")+"\t");
            List<Slja.GSMUtility.Model.sms_MsgSend> modellist = new List<Slja.GSMUtility.Model.sms_MsgSend>();
            try
            {
                for (int k = 0; k < sms_Reciver.Count; k++)
                {
                    Slja.GSMUtility.Model.sms_MsgSend send = new Slja.GSMUtility.Model.sms_MsgSend();
                    send.MsgContent = Msg;
                    send.RecvNO = sms_Reciver[k];
                    send.SendStatus = 0;
                    send.SendTime = DateTime.Now;
                    send.SendUser = SendUser;
                    modellist.Add(send);
                }
                Slja.GSMUtility.DAL.sms_MsgSend dal = new Slja.GSMUtility.DAL.sms_MsgSend();
                dal.Add(modellist);
              
            }
            catch (Exception err)
            {
                logger.Error(err.Message + err.StackTrace);
            }
            return modellist;//将短信返回（包含Id列）
        }
        [WebMethod]
        public List<Slja.GSMUtility.Model.sms_MsgRecive> ReciveNewMsg()
        {            
            Slja.GSMUtility.DAL.sms_MsgRecive recv = new Slja.GSMUtility.DAL.sms_MsgRecive();
            return recv.GetNewMsg();
        }
         [WebMethod]
        public int MsgRead(int recvmsgModelID,string UserName)
        {
            Slja.GSMUtility.DAL.sms_MsgRecive dal = new Slja.GSMUtility.DAL.sms_MsgRecive();
            return dal.Read(recvmsgModelID, UserName);
        }
         [WebMethod]
         public List<MsgStatus> GetMsgSendStatus(int[] MsgIDs)
         {
             Slja.GSMUtility.DAL.sms_MsgSend dal = new Slja.GSMUtility.DAL.sms_MsgSend();
             List<MsgStatus> list = dal.GetMsgSendStatus(MsgIDs);                    
             return list;
         }
         [WebMethod]
         public string GetXmlFromMsgIndexInfo(string UserID)
         {
             string xMsgIndexInfo = Slja.GSMUtility.SmsUti.GetXMLFromMsgIndexInfo(UserID);
             return xMsgIndexInfo;
         }
         [WebMethod]
         public string GetXmlFromLinkmanMobileNO(string UserID,string MobileNO)
         {
             string xMsgDetailInfo = Slja.GSMUtility.SmsUti.GetXMLFromLinkmanMobileNO(UserID, MobileNO);
             return xMsgDetailInfo;
         }
    }


}